######################################
#模块概述：员工可以创建自己的个人档案
#作者：刘卓柳
#创建时间：2021.3.6
#更新时间：2021.3.15
######################################
# 导入数据库模块
import pymysql
# 导入Flask框架，这个框架可以快捷地实现了一个WSGI应用
from flask import Flask
from flask import flash
# 默认情况下，flask在程序文件夹中的templates子文件夹中寻找模块
from flask import render_template
# 导入前台请求的request模块
from flask import request,Blueprint,session
import traceback

# 传递根目录
createWI=Blueprint('createWI',__name__)

######################################
#函数概述：显示创建个人档案页面
######################################
@createWI.route('/createWorkerInfo', methods=['get', 'post'])
def createWorkerInfo():
    if session.get('isadminan') != '员工':
        flash("您没有此操作权限！" )
        return render_template('homepage.html')
    wid = session.get('uid')
    conn = pymysql.connect(host='139.217.107.173', user='root', password='123456', db='project_training',
                           charset='utf8')
    cur = conn.cursor()
    # SQL 查询语句
    sql = "select* from workerinfo WHERE ID ='%s'" % (wid)
    cur.execute(sql)
    content = cur.fetchall()
    conn.close()
    if len(content)==1:
        flash("您已创建员工档案，请勿重复操作！")
        return render_template('homepage2.html')
    else:
        return render_template('createWorkerInfo.html')

######################################
#函数概述：提交创建个人档案页语句
######################################
@createWI.route('/submitWorkerInfo', methods=['get', 'post'])
def submitWorkerInfo():
    id=session['uid']
    name = request.values.get('name')
    sex = request.values.get('sex')
    birthday = request.values.get('birthday')
    city = request.values.get('city')
    tel = request.values.get('tel')
    email = request.values.get('email')
    workstate = request.values.get('workstate')
    cname = request.values.get('cname')
    major = request.values.get('major')
    school = request.values.get('school')
    education = request.values.get('education')

    conn = pymysql.connect(host='139.217.107.173', user='root', password='123456', db='project_training',
                           charset='utf8')
    cur = conn.cursor()
    # SQL 查询语句
    sql = "INSERT INTO workerinfo(ID,Name,Sex,Birthday,City,tel,Email,WorkState,CName,Major,School,Education) value('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (id,name, sex, birthday, city, tel, email, workstate, cname, major, school, education)

    cur.execute(sql)
    conn.commit();
    conn.close()

    return render_template('homepage2.html')
